package com.cxg.fairy.ec.launcher;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.app.IUserChecker;
import com.cxg.fairy.delegates.FairyDelegate;
import com.cxg.fairy.ec.R;
import com.cxg.fairy.ui.launcher.ILauncherListener;
import com.cxg.fairy.ui.launcher.LauncherHolderCreator;
import com.cxg.fairy.ui.launcher.OnLauncherFinishTag;
import com.cxg.fairy.ui.launcher.ScrollLauncherTag;
import com.cxg.fairy.util.storage.FairyPreference;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/11/3.
 */

public class LauncherScrollDelegate extends FairyDelegate implements OnItemClickListener{

    private ConvenientBanner<Integer> mConvenientBanner = null;
    private static final ArrayList<Integer> MCCOUNT = new ArrayList<>();
    private ILauncherListener mIlauncherListener = null;

    private void initBanner() {
        MCCOUNT.add(R.mipmap.launcher_fairy_01);
        MCCOUNT.add(R.mipmap.launcher_fairy_02);
        MCCOUNT.add(R.mipmap.launcher_fairy_03);
        MCCOUNT.add(R.mipmap.launcher_fairy_04);
        mConvenientBanner
                .setPages(new LauncherHolderCreator(), MCCOUNT)
                .setPageIndicator(new int[]{R.drawable.dot_normal, R.drawable.dot_focus})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setOnItemClickListener(this)
                .setCanLoop(false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ILauncherListener) {
            mIlauncherListener = (ILauncherListener) activity;
        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initBanner();
    }

    @Override
    public Object setLayout() {
        mConvenientBanner = new ConvenientBanner<>(getContext());
        return mConvenientBanner;
    }

    @Override
    public void onItemClick(int position) {
        //if the click on last page
    if (position == MCCOUNT.size() - 1) {
            FairyPreference.setAppFlag(ScrollLauncherTag.HAS_FIRST_LAUNCHER_APP.name(),true);
        //check if logined
        AccountManager.checkAccount(new IUserChecker() {
            @Override
            public void onSignIn() {
                if (mIlauncherListener != null) {
                    mIlauncherListener.OnLauncherFinish(OnLauncherFinishTag.SIGNED);
                }
            }

            @Override
            public void onNotSignIn() {
                if (mIlauncherListener != null) {
                    mIlauncherListener.OnLauncherFinish(OnLauncherFinishTag.NOT_SIGNED);
                }
            }
        });
        }
    }
}
